﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Threading;
using DotNetNuke.Entities.Host;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using DotNetNuke.Services.Messaging.Data;


namespace DotNetNuke.Services.Messaging.Scheduler
{

	public class MessagingScheduler : DotNetNuke.Services.Scheduling.SchedulerClient
	{

		AsyncCompletedEventArgs asyncCompletedEventArgs;
		AutoResetEvent waitHandle;
		PortalController _pController = new PortalController();
		MessagingController _mController = new MessagingController();

		UserController _uController = new UserController();

		public MessagingScheduler(DotNetNuke.Services.Scheduling.ScheduleHistoryItem objScheduleHistoryItem)
			: base()
		{
			this.ScheduleHistoryItem = objScheduleHistoryItem;
			waitHandle = new AutoResetEvent(false);
		}

		public override void DoWork()
		{

			try
			{
				Guid _schedulerInstance = Guid.NewGuid();
				this.ScheduleHistoryItem.AddLogNote("MessagingScheduler DoWork Starting " + _schedulerInstance.ToString());

				if ((string.IsNullOrEmpty(Host.SMTPServer)))
				{
					this.ScheduleHistoryItem.AddLogNote("No SMTP Servers have been configured for this host. Terminating task.");
					this.ScheduleHistoryItem.Succeeded = true;
					//'Return
				}
				else
				{
					Hashtable settings = this.ScheduleHistoryItem.GetSettings();

					bool _messageLeft = true;
					int _messagesSent = 0;


					while (_messageLeft)
					{
						Message currentMessage = _mController.GetNextMessageForDispatch(_schedulerInstance);

						if ((currentMessage != null))
						{
							try
							{
								SendMessage(currentMessage);
								_messagesSent = _messagesSent + 1;
							}
							catch (Exception e)
							{
								this.Errored(ref e);
							}
						}
						else
						{
							_messageLeft = false;
						}

					}

					this.ScheduleHistoryItem.AddLogNote(string.Format("Message Scheduler '{0}' sent a total of {1} message(s)", _schedulerInstance, _messagesSent));
					this.ScheduleHistoryItem.Succeeded = true;

				}

			}
			catch (Exception ex)
			{
				this.ScheduleHistoryItem.Succeeded = false;
				this.ScheduleHistoryItem.AddLogNote("MessagingScheduler Failed: " + ex.ToString());
				this.Errored(ref ex);
			}
		}

		private void SendMessage(Message objMessage)
		{
			string senderAddress = UserController.GetUserById(objMessage.PortalID, objMessage.FromUserID).Email;
			string fromAddress = _pController.GetPortal(objMessage.PortalID).Email;
			string toAddress = _uController.GetUser(objMessage.PortalID, objMessage.ToUserID).Email;


			Mail.Mail.SendEmail(fromAddress, senderAddress, toAddress, objMessage.Subject, objMessage.Body);

			_mController.MarkMessageAsDispatched(objMessage.MessageID);

		}
	}



}
